package com.example.group.network.express_plus_app;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.example.group.network.express_plus_app.bean.MyResponse;
import com.example.group.network.express_plus_app.bean.Order;
import com.example.group.network.express_plus_app.http.GsonRequest;
import com.example.group.network.express_plus_app.http.MySingleton;
import com.example.group.network.express_plus_app.ui.login.LoginActivity;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MyPostShowDetailActivity extends AppCompatActivity implements View.OnClickListener {
    @BindView(R.id.show_user_phone)
    TextView show_user_phone;
    @BindView(R.id.show_express_type)
    TextView show_express_type;
    @BindView(R.id.show_estimated_price)
    TextView show_estimated_price;
    @BindView(R.id.show_stuff_detail)
    TextView show_stuff_detail;
    @BindView(R.id.show_delivery_mode)
    TextView show_delivery_mode;
    @BindView(R.id.show_express_detail_address)
    TextView show_express_detail_address;
    @BindView(R.id.show_delivery_address)
    TextView show_delivery_address;
    @BindView(R.id.show_user_leisure)
    TextView show_user_leisure;
    @BindView(R.id.show_order_id)
    TextView show_order_id;
    @BindView(R.id.show_order_create_time)
    TextView show_order_create_time;

    RequestQueue queue;
    HashMap<String, String> mInfoMap;
    String server_ip ;
    Order order;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_post_show_detail);
        ButterKnife.bind(this);
        setActionBar("信息详细");
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        order=(Order) bundle.getSerializable("ORDER");
        show_user_phone.setText(order.getUserPhone());
        show_express_type.setText(order.getExpressType());
        show_estimated_price.setText(String.valueOf(order.getEstimatedPrice()));
        show_stuff_detail.setText(order.getStuffDetail());
        show_delivery_mode.setText(order.getDeliveryMode());
        show_express_detail_address.setText(order.getExpressDetailAddress());
        show_delivery_address.setText(order.getDeliveryAddress());
        show_user_leisure.setText(order.getUserLeisure());
        show_order_id.setText(order.getOrderId());
        String[] split = order.getOrderCreateTime().split("\\.");
        Log.i("split",split[1]);
        show_order_create_time.setText(split[0]);
        findViewById(R.id.btn_cancel_order).setOnClickListener(this);

        queue= MySingleton.getInstance(getApplicationContext()).getRequestQueue();
        server_ip=getApplication().getString(R.string.server_ip);
        mInfoMap = MainApplication.getInstance().mInfoMap;


    }
    public void setActionBar(String title){
        //标题栏设置
        ActionBar supportActionBar = getSupportActionBar();
        supportActionBar.setDisplayHomeAsUpEnabled(true);
        supportActionBar.setHomeAsUpIndicator(R.drawable.ic_baseline_arrow_back_ios_24);
        supportActionBar.setTitle(title);

    }
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId()==android.R.id.home){
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void deleteMyOrderByOrderId(String orderId){
        String token = mInfoMap.get("token");
        String url=server_ip+"express-order/order/delete-my-order-by-order-id?orderId="
                +orderId;
        Map<String,String> head=new HashMap<>();
        head.put("Authorization","Bearer "+token);
        GsonRequest<MyResponse> userGsonRequest=new GsonRequest<MyResponse>(url, Request.Method.POST, MyResponse.class, head, new Response.Listener<MyResponse>() {
            @Override
            public void onResponse(MyResponse response) {
                Log.i("收货地址",response.getStatusCode().toString());
                if (response.getOk()){
                    Toast.makeText(MyPostShowDetailActivity.this, "订单已取消成功", Toast.LENGTH_SHORT).show();
                    finish();
                }else {
                    Toast.makeText(MyPostShowDetailActivity.this, "服务器错误！", Toast.LENGTH_SHORT).show();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.i("setting 收货地址","更新失败");
                Toast.makeText(MyPostShowDetailActivity.this, "请检查网络是否连接！", Toast.LENGTH_SHORT).show();

            }
        });
        userGsonRequest.setRetryPolicy(new DefaultRetryPolicy(6000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        queue.add(userGsonRequest);
    }

    @Override
    public void onClick(View v) {
        if (v.getId()==R.id.btn_cancel_order){

            deleteMyOrderByOrderId(order.getOrderId());
        }
    }
}